﻿<%@ Control Language="C#" AutoEventWireup="true" Inherits="Directory.Web.Controls_CascadingCategory" Codebehind="CascadingCategory.ascx.cs" %>

<input type="hidden" id="IdContainer" value="<% =this.tbSelectedId.ClientID %>" />
<input type="hidden" id="responseUrl" value="<% =this.ResponseUrl %>" />
<input type="hidden" id="tbSelectedId" runat="server" />
<input type="hidden" id="isAutoLoad" value="<% =this.AutoLoad %>" />
<input type="hidden" id="selectedIndex" value="<% =this.SelectedIndexStr %>" />
<div id="selectPart">
	<asp:Literal ID="ltInit" runat="server"></asp:Literal>
</div>

<script type="text/javascript">
	var counter = 0;
	function SetValue() {
		var currentselection = document.getElementById('selectPart').lastChild;
		var selected = false;
		while (!selected) {
			if (currentselection == null) break;
			if (currentselection.tagName == "SELECT") {
				if (currentselection.selectedIndex != 0) {
					selected = true;
					break;
				}
			}
			currentselection = currentselection.previousSibling;
		}
		if (selected == true) {
			document.getElementById(document.getElementById('IdContainer').value).value = currentselection.value;
		}
	}
	function SelectChange(obj, func) {
		var sibling = obj.nextSibling;
		while (sibling != null) {
			var cur = sibling;
			sibling = sibling.nextSibling;
			document.getElementById('selectPart').removeChild(cur);
		}
		SetValue();
		if (obj.selectedIndex != 0) {
			GetNextOption(parseInt(obj.value),func);
		}
	}
	function onSelectChange() {
		SelectChange(this,null);
	}
	function GetNextOption(id,func) {
		var xmlhttp = Ajax_GetXMLHttpRequest();
		xmlhttp.onreadystatechange = function() {
			if (xmlhttp.readyState == 4) {
				if (xmlhttp.status == 200) {
					if (xmlhttp.responseXML) {
						AddChild(xmlhttp.responseXML);
						if (func != null) {
							func();
						}
					} else {
						//失败
					}
				}
			}
		}
		var time = new Date();
		xmlhttp.open("GET", document.getElementById('responseUrl').value + "?timestamp=" + time.getTime() + "&action=getcateogrychilds&categoryId=" + id, true);
		xmlhttp.send(null);
	}

	function AddChild(xmlDoc) {
		var selectionList = xmlDoc.getElementsByTagName("select");
		counter++;
		for (var j = 0; j < selectionList.length; j++) {
			var newselection = document.createElement("select");
			newselection.setAttribute("id", '_select_' + counter);
			newselection.setAttribute("name", '_select_' + counter);
			var optionList = selectionList[j].childNodes;
			for (var i = 0; i < optionList.length; i++) {
				if (optionList[i].tagName == 'option') {
					var option = document.createElement("option");
					option.value = optionList[i].getAttribute("CategoryId");
					var text = document.createTextNode(decodeURI(optionList[i].getAttribute("CategoryName")));
					option.appendChild(text);
					if (parseInt(optionList[i].getAttribute("IsSelected")) == 1) {
						option.setAttribute("selected", "selected");
					}
					newselection.appendChild(option);
				}
				continue;
			}
			newselection.onchange = onSelectChange;
			document.getElementById('selectPart').appendChild(newselection);
		}
	}
/******* 自动加载  *********/
	var indexStr = document.getElementById('selectedIndex').value;
	var indexArray = indexStr.split(",");
	var autoloadCount = 0;
	var stopLoad = false;
	function AutoLoad() {
		if (stopLoad == false) {
			if (autoloadCount < indexArray.length) {
				autoloadCount++;
				var i = autoloadCount - 1;
				SetSelected(i, indexArray[i]);
			} else {
				stopLoad = true;
			}
		}
	}

	function SetSelected(selectIndex, selectedValue) {
		var select = GetSelect(selectIndex);
		if (select == null) {
			stopLoad = true;
			return;
		}
		if (SetSelectedOption(select, selectedValue) == true) {
			SelectChange(select, AutoLoad);
		} else {
			stopLoad = true;
		}
	}

	function GetSelect(selectIndex) {
		var c = 0;
		var child = document.getElementById('selectPart').firstChild;
		while (c <= selectIndex) {
			if (child.tagName == "SELECT") {
				if (c == selectIndex) {
					break;
				}
				c++;
			}
			child = child.nextSibling;
			if (child == null) {
				return null;
			}
		}
		if (c == selectIndex) {
			if (child.tagName == "SELECT") {
				return child;
			}
		}
		return null;
	}

	function SetSelectedOption(obj, selectedValue) {
		if (obj != null) {
			if (obj.tagName == "SELECT") {
				for (var i = 0; i < obj.options.length; i++) {
					if (obj.options[i].value == selectedValue) {
						obj.options[i].selected = true;
						return true;
					}
				}
			}
		}
		return false;
	}

	if (window.onload != null) {
	}

	function Load() {
		if (document.getElementById('isAutoLoad').value.toLowerCase() == 'true') {
			AutoLoad();
		}
	}

	//AddOnloadEvent(Load);
	Load();
/******** 自动加载 ***********/
</script>